﻿using DTO;
using IService;
using Service.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Common;


namespace Service
{
    public class JobService: IJobService
    {
        public List<JobDTO> GetList()
        {
            using (var db = new OAContext())
            {
                BaseService<Job> bs = new BaseService<Job>(db);
                var list = bs.Select(u => true).Select(m => new JobDTO
                {
                    Id = m.Id,
                    Name = m.Name,
                    Code = m.Code
                }).ToList();

                return list;
            }
        }

        public JobDTO JobQueryId(string Id)
        {
            using (var db=new OAContext()) {
                BaseService<Job> bs = new BaseService<Job>(db);
                var whereLambda = Common.PredicateExtensions.True<Job>();
                whereLambda = whereLambda.And(u=>u.Id==Id);
                var model = bs.Get(whereLambda);
                JobDTO job = new JobDTO();
                job.Id = model.Id;
                job.Name = model.Name;
                job.Code = model.Code;
                job.IsDel = model.IsDel;
                job.UpdateTime = model.UpdateTime;
                job.CreateTime = model.CreateTime;

                return job;
            }
        }

        public bool Add(JobDTO entity) 
        {
            using (var db = new OAContext())
            {
                BaseService<Job> bs = new BaseService<Job>(db);
                Job job = new Job();
                job.Id = Guid.NewGuid().ToString();
                job.Name = entity.Name;
                job.Code = entity.Code;
                job.IsDel = 1;
                job.CreateTime = DateTime.Now;
                job.UpdateTime = null;
                return bs.Add(job);
            }
        }

        public bool Del(JobDTO entity)
        {
            using (var db = new OAContext())
            {
                BaseService<Job> bs = new BaseService<Job>(db);
                var model = db.Job.Find(entity.Id);
                model.IsDel = 0;

                return bs.Del(model);
            }
        }
        public bool Update(JobDTO entity)
        {
            using (var db = new OAContext())
            {
                BaseService<Job> bs = new BaseService<Job>(db);
                var model = db.Job.Find(entity.Id);
                model.Name = entity.Name;
                model.Code = entity.Code;
                model.UpdateTime = DateTime.Now;
                return bs.Update(model);
            }
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="limit">页码</param>
        /// <param name="offset">每页显示的数据</param>
        /// <param name="rowCount">总数量</param>
        /// <returns></returns>
        public List<JobDTO> ShowJobPageList(int offset,int limit, string name,string code,  ref int rowCount, string sort, string order)
        {
            using (var db = new OAContext())
            {
                BaseService<Job> bs = new BaseService<Job>(db);
                rowCount = 0;

                //Expression<Func<Job, bool>> where = p => p.IsDel == 1;
                var query = Common.PredicateExtensions.True<Job>();
                if (!string.IsNullOrWhiteSpace(name))
                {
                    query = query.And<Job>(u => u.Name.Contains(name));

                    //where = where.And(u=>u.Name.Contains(name));
                }
                if (!string.IsNullOrWhiteSpace(code))
                {
                    query = query.And<Job>(u => u.Code.Contains(code));
                    //where = where.And(u=>u.Code.Contains(code));
                }
                var orderBy = Common.PredicateExtensions.GetSortLambda<Job>(sort);

                bool orderType = order == "asc" ? true : false;

                var list = bs.PageSizeSelect(offset, limit, ref rowCount, query, orderBy, orderType).Select(m => new JobDTO
                {
                    Id = m.Id,
                    Name = m.Name,
                    Code = m.Code
                }).ToList();
                return list;
            }
        }
    }
}
